OpenCV how to open a image with Python and C++

2021, Dec 06

Here is an example of opening an image with OpenCV in both Python and C++.

I purposely kept it simple so as not to confuse anyone and make it easy to cut and paste code for getting started.

Overview diagram of the project

Image input and what is expected as output diagram
Overview

Python code

import cv2

img = cv2.imread("flower.jpg")

cv2.imshow("Flowers", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

C++ code

#include <opencv2/highgui.hpp>

int main() {
  cv::Mat img = cv::imread("flower.jpg");

  cv::imshow("Display window", img);
  cv::waitKey(0);
  cv::destroyAllWindows();

  return 0;
}